package com.yanggeng.demo.beginnerguidedemo.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.github.mikephil.charting.utils.Utils;
import com.yanggeng.demo.beginnerguidedemo.bean.PieEntry;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义饼图
 * @author aiden@tronsis.com
 * @date 2018/5/15 18:36
 */
public class PieChartView extends View {

    private List<Integer> mColors;
    private List<PieEntry> data = new ArrayList<>();
    private int mDefaultColor = 0x333333;
    private Paint mPaint;
    private int mWidth, mHeight;
    private float mRadius;
    private float mSum = 0f;
    private float mCurrentAngle = 0f;

    public PieChartView(Context context) {
        super(context);
    }

    public PieChartView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PieChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);
    }

//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        int size = (int) Utils.convertDpToPixel(50f);
//        setMeasuredDimension(
//                Math.max(getSuggestedMinimumWidth(),
//                        resolveSize(size,
//                                widthMeasureSpec)),
//                Math.max(getSuggestedMinimumHeight(),
//                        resolveSize(size,
//                                heightMeasureSpec)));
//    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (data == null || data.size() <= 0) {
            mPaint.setColor(mDefaultColor);
            canvas.drawCircle(mWidth / 2, mHeight / 2, mRadius, mPaint);
        } else {
            getSum();
            canvas.translate(mWidth / 2, mHeight / 2);
            mRadius = Math.min(mWidth, mHeight) / 2;
            RectF rectF = new RectF(-mRadius, -mRadius, mRadius, mRadius);
            mCurrentAngle = -90;
            for (int i = 0; i < data.size(); i++) {
                mPaint.setColor(mColors.get(i));
                float sweepAngle = data.get(i).getValue() / mSum * 360;
                canvas.drawArc(rectF, mCurrentAngle, sweepAngle - 1, true, mPaint);
                mCurrentAngle += sweepAngle;
            }
        }
    }

    private void getSum() {
        mSum = 0;
        for (int i = 0; i < data.size(); i++) {
            mSum += data.get(i).getValue();
        }
    }

    public void setData(List<PieEntry> data) {
        this.data.clear();
        this.data.addAll(data);
        invalidate();
    }

    public void setColors(List<Integer> colors) {
        this.mColors = colors;
    }
}
